package com.maciqing.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @author maciqing
 * @create 2021-02-02 21:57
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer>{

    private Set<Integer> set= new HashSet<>();
    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for(int val: vals){
            set.add(val);
        }
    }

    //判斷是否校驗成功
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);//value需要校驗的值
    }
}
